#include <fstream>
#include <iostream>

using namespace std;


void read(ifstream &T){	
	FILE* fileOutputStreamTemp = fopen ("parseSms.temp", "wt");
	char ch;
	while(!T.eof()){
		T.get(ch);
		int  newkey   = ch;
		char ASCII    = newkey & 0xff;
		if(ASCII >= 32 && ASCII <= 126){
			fprintf (fileOutputStreamTemp, "%s", ch);
		}
		else{
			fprintf (fileOutputStreamTemp, " ");
		}
	}
}


int main ( int argc, char *argv[] ){
	const int arraySizeIncrease = 1000000;
	int arraySize = arraySizeIncrease, wordCount = 0, numCount = 0;
	char** wordListArray = new char* [arraySize];
	char wordSize [512];
	int numSize [512];
	ifstream T( argv[1] );
	read(T);
	T.close();

	if ( argc != 3 ) {
		cout<<"usage: "<< argv[0] <<" <input_filename> <output_filename>\n";
	}
	else {
		ifstream the_file ( "parseSms.temp"  );
		if ( !the_file.is_open() ){
			cout<<"Could not open file\n";
		}
		else {
			while (the_file >> wordSize ){
				wordListArray[wordCount] = new char [strlen (wordSize) + 1];
				strcpy (wordListArray[wordCount], wordSize);
				wordCount++;
				//If the File has more then 1000000 Words!
				if (wordCount % arraySizeIncrease == 0){
					char** oldSize = wordListArray;
					arraySize += arraySizeIncrease;
					wordListArray = new char* [arraySize];
					memcpy (wordListArray, oldSize, wordCount * sizeof(char*));
					delete oldSize;
				}
			}
		}
		the_file.close();
	}
	FILE* fileOutputStream = fopen (argv[2], "wt");
	for (int cntIndex = 0; cntIndex < wordCount; cntIndex++){
		fprintf (fileOutputStream, "%s\n", wordListArray[cntIndex]);
	}
	fclose (fileOutputStream);
	for (int cntIndexN = 0; cntIndexN < wordCount; cntIndexN++){
		delete wordListArray[cntIndexN];
	}
	delete wordListArray;
	wordListArray = NULL;
	cout << "\nOutput file Written.\n\n";
}

